﻿using System.Web.Mvc;

namespace WebApp.Common
{
    public class AuthFilter : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            var contentType = filterContext.HttpContext.Request.ContentType.ToLower();
            if((contentType == "application/json; charset=utf-8" || contentType == "application/xml; charset=utf-8")  && !filterContext.IsChildAction)
            {
                filterContext.Result = new HttpStatusCodeResult(401);
                filterContext.HttpContext.Response.StatusCode = 401;
                filterContext.HttpContext.Response.SuppressContent = true;
                filterContext.HttpContext.Response.End();
            }
            else
                base.HandleUnauthorizedRequest(filterContext);
        }
    }
}